/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Rozvrh;

import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.fop.apps.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


import org.apache.fop.apps.MimeConstants;
import org.apache.xmlgraphics.util.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
 *Trieda umožňujúca export rozvrhu do formátu PDF. Dokáže vytvárať XSL-FO, k ktorého sa 
 * pomocou funkcií knižnice FOP transformuje na PDF.
 * @author MartinaHlinková
 */
public class GeneratorDokumentu {
    private Rozvrh.RozvrhMesiaca rozvrh;
    private DOMSource source;
    private String nazovExportu;
    
    private final int FONT_SIZE = 8;
    private final int CELL_WIDTH_MENO = 20;
    private final int CELL_WIDTH_HOD = 10;
    private final int ROW_HEIGHT_ZAM = 6;
    
    /**
     * Konštruktor objektov GeneratorDokumentu, ktorého objekty sa dokážu pretransformovať. 
     * @param rozvrh    rozvrh, ktorý sa má vypísať do PDF
     * @param nazov     názov dokumentu PDF, do ktorého sa má exportovať
     */
    public GeneratorDokumentu(Rozvrh.RozvrhMesiaca rozvrh,String nazov){
        this.rozvrh = rozvrh;
        this.source = null;
        this.nazovExportu = nazov+rozvrh.getInfo().getMasiac().toString()+rozvrh.getInfo().getRok();
       
    }
    
    /**
     * Metóda, ktorá vygeneruje tabuľku s informáciami o rozvrhu vo fotmáte XSL-FO. 
     * Informácie: Adresa komunitného centra, optimálny počet odpracovaných hodín
     * za mesiac, rok, mesiac, názvy stĺpcov, počty dní
     * @param zapisovanyRozvrh  rozvrh, z ktorého sa zistia informácie
     * @param doc   dokumentová štruktúra, kam sa má uložiť tabuľka s informáciami
     * @return prvý element tabuľky
     */
    public Element generujFOTabulkuInfo(Rozvrh.RozvrhMesiaca zapisovanyRozvrh,Document doc){
        if(doc == null || zapisovanyRozvrh == null)return null;
        Element elementTableInfo = doc.createElement("fo:table");
        elementTableInfo.setAttribute("font-family", "sans-serif");
        elementTableInfo.setAttribute("font-size", "11pt");
        elementTableInfo.setAttribute("font-weight", "bold");
        elementTableInfo.setAttribute("table-layout", "fixed");
        
            //zaznamenanie poctu a siriek stlpcov
            int pocetDni = zapisovanyRozvrh.getInfo().getPocetDni();
            int sirkaBunky = (int)(297-4*CELL_WIDTH_HOD-CELL_WIDTH_MENO)/pocetDni;
            int pocetBuniek = pocetDni+5; 
            for(int i=1; i<=pocetBuniek;i++){
                Element elementTableInfoCol = doc.createElement("fo:table-column");
                elementTableInfoCol.setAttribute("column-number", ""+i);
                int sirkaStlpca = sirkaBunky;
                if(i == 1) sirkaStlpca = CELL_WIDTH_MENO;
                else if(i>=2 && i<=3) sirkaStlpca = CELL_WIDTH_HOD;
                else if(i>3+pocetDni) sirkaStlpca = CELL_WIDTH_HOD;
                
                elementTableInfoCol.setAttribute("column-width", ""+sirkaStlpca+"mm");
                elementTableInfo.appendChild(elementTableInfoCol);
            }
            
        Element elementTableInfoBody = doc.createElement("fo:table-body");
        elementTableInfoBody.setAttribute("border-style", "solid");
        elementTableInfoBody.setAttribute("border-width", "0.1pt");
            
        //prvy riadok tabulky
        
            Element elementTableRow1 = doc.createElement("fo:table-row");
            elementTableRow1.setAttribute("font-weight", "bold");
            
                Element elementRow1Cell1 = doc.createElement("fo:table-cell");
                elementRow1Cell1.setAttribute("number-columns-spanned", ""+13);
                elementRow1Cell1.setAttribute("border-style", "solid");
                
                    Element row1Cell1 = doc.createElement("fo:block");
                    row1Cell1.setAttribute("text-align", "center");
                    row1Cell1.setTextContent("Komunitne centrum Drahuskovo, 916 16 Krajne 828");
                    elementRow1Cell1.appendChild(row1Cell1);
                
                elementTableRow1.appendChild(elementRow1Cell1);
                
                Element elementRow1Cell2 = doc.createElement("fo:table-cell");
                elementRow1Cell2.setAttribute("number-columns-spanned", ""+11);
                elementRow1Cell2.setAttribute("border-style", "solid");
                
                    Element row1Cell2 = doc.createElement("fo:block");
                    row1Cell2.setAttribute("text-align", "center");
                    InformacnaStruktura.CasBezDatumu casFPC = new InformacnaStruktura.CasBezDatumu(zapisovanyRozvrh.getInfo().getOptimalnyPocetMinut());
                    row1Cell2.setTextContent("Fond pracovneho casu - "+casFPC);
                    elementRow1Cell2.appendChild(row1Cell2);
                
                elementTableRow1.appendChild(elementRow1Cell2);
                
                Element elementRow1Cell3 =  doc.createElement("fo:table-cell");
                int zvysneStlpce = pocetBuniek-24;
                elementRow1Cell3.setAttribute("number-columns-spanned", ""+zvysneStlpce);
                elementRow1Cell3.setAttribute("border-style", "solid");
                
                    Element row1Cell3 = doc.createElement("fo:block");
                    row1Cell3.setAttribute("text-align", "center");
                    String mesiacString = ""+zapisovanyRozvrh.getInfo().getMasiac().toString()+" "+zapisovanyRozvrh.getInfo().getRok();
                    row1Cell3.setTextContent(""+mesiacString);
                    elementRow1Cell3.appendChild(row1Cell3);
                
                elementTableRow1.appendChild(elementRow1Cell3); 
                
            elementTableInfoBody.appendChild(elementTableRow1);
            
            //druhy riadok info tabulky
            Element elementTableRow2 = doc.createElement("fo:table-row");   
            
                Element elementRow2Cell1 =  doc.createElement("fo:table-cell");
                elementRow2Cell1.setAttribute("number-rows-spanned", ""+3);
                elementRow2Cell1.setAttribute("border-style", "solid");
                
                    Element row2Cell1 = doc.createElement("fo:block");
                    elementRow2Cell1.appendChild(row2Cell1);
                
                elementTableRow2.appendChild(elementRow2Cell1);
                
                Element elementRow2Cell2 =  doc.createElement("fo:table-cell");
                elementRow2Cell2.setAttribute("number-rows-spanned", ""+2);
                elementRow2Cell2.setAttribute("number-columns-spanned", ""+2);
                elementRow2Cell2.setAttribute("border-style", "solid");
                
                    Element row2Cell2 = doc.createElement("fo:block");
                    row2Cell2.setAttribute("text-align", "center");
                    row2Cell2.setAttribute("reference-orientation", "90");
                    row2Cell2.setTextContent("Prevod hodin z minulosti");
                    elementRow2Cell2.appendChild(row2Cell2);
                
                elementTableRow2.appendChild(elementRow2Cell2);
                
               
                
                Element elementRow2Cell4 =  doc.createElement("fo:table-cell");
                elementRow2Cell4.setAttribute("number-columns-spanned", ""+pocetDni);
                elementRow2Cell4.setAttribute("border-style", "solid");
                    
                    Element row2Cell4 = doc.createElement("fo:block");
                    row2Cell4.setAttribute("text-align", "center");
                    row2Cell4.setTextContent(this.nazovExportu);
                    elementRow2Cell4.appendChild(row2Cell4);
                
                elementTableRow2.appendChild(elementRow2Cell4);
                
               
                
                Element elementRow2Cell6 =  doc.createElement("fo:table-cell");
                elementRow2Cell6.setAttribute("number-rows-spanned", ""+3);
                elementRow2Cell6.setAttribute("border-style", "solid");
                
                    Element row2Cell6 = doc.createElement("fo:block");
                    row2Cell6.setAttribute("text-align", "center");
                    row2Cell6.setAttribute("reference-orientation", "90");
                    row2Cell6.setTextContent("Hod +");
                    elementRow2Cell6.appendChild(row2Cell6);
                
                elementTableRow2.appendChild(elementRow2Cell6);
                
                Element elementRow2Cell7 =  doc.createElement("fo:table-cell");
                elementRow2Cell7.setAttribute("number-rows-spanned", ""+3);
                elementRow2Cell7.setAttribute("border-style", "solid");
                
                    Element row2Cell7 = doc.createElement("fo:block");
                    row2Cell7.setAttribute("text-align", "center");
                    row2Cell7.setAttribute("reference-orientation", "90");
                    row2Cell7.setTextContent("Hod -");
                    elementRow2Cell7.appendChild(row2Cell7);
                
                elementTableRow2.appendChild(elementRow2Cell7);
                
               
            
            elementTableInfoBody.appendChild(elementTableRow2);
            
            //treri riadok tabulky
            Element elementTableRow3 = doc.createElement("fo:table-row");
            
                Element elementRow3Cell1 =  doc.createElement("fo:table-cell");
                elementRow3Cell1.setAttribute("number-columns-spanned", ""+pocetDni);
                elementRow3Cell1.setAttribute("border-style", "solid");
                
                    Element row3Cell1 = doc.createElement("fo:block");
                    row3Cell1.setAttribute("text-align", "center");
                    row3Cell1.setTextContent("Kalendarne dni mesiaca");
                    elementRow3Cell1.appendChild(row3Cell1);
                
                elementTableRow3.appendChild(elementRow3Cell1);
            
            elementTableInfoBody.appendChild(elementTableRow3);
            
            //stvrtyRiadok tabulky
            Element elementTableRow4 = doc.createElement("fo:table-row");
            
                Element elementRow4Cell1 =  doc.createElement("fo:table-cell");
                elementRow4Cell1.setAttribute("border-style", "solid");
                    
                    Element row4Cell1 = doc.createElement("fo:block");
                    row4Cell1.setAttribute("text-align", "center");
                    row4Cell1.setTextContent("+");
                    elementRow4Cell1.appendChild(row4Cell1);
                
                elementTableRow4.appendChild(elementRow4Cell1);
                
                Element elementRow4Cell2 =  doc.createElement("fo:table-cell");
                elementRow4Cell2.setAttribute("border-style", "solid");
                    
                    Element row4Cell2 = doc.createElement("fo:block");
                    row4Cell2.setAttribute("text-align", "center");
                    row4Cell2.setTextContent("-");
                    elementRow4Cell2.appendChild(row4Cell2);
                
                elementTableRow4.appendChild(elementRow4Cell2);
                
                for(int i=1; i<=pocetDni;i++){
                    Element elementRow4Cell3 =  doc.createElement("fo:table-cell");
                    elementRow4Cell3.setAttribute("border-style", "solid");
                    
                        Element row4Cell3 = doc.createElement("fo:block");
                        row4Cell3.setAttribute("text-align", "center");
                        row4Cell3.setTextContent(""+i);
                        elementRow4Cell3.appendChild(row4Cell3);
                
                    elementTableRow4.appendChild(elementRow4Cell3);
                }
            
            elementTableInfoBody.appendChild(elementTableRow4);
        
        elementTableInfo.appendChild(elementTableInfoBody);
        
        return elementTableInfo;
    }
    
    /**
     * Metóda, ktorá vygeneruje tabuľku s rozvrhom vo fotmáte XSL-FO. 
     * @param zapisovanyRozvrh  rozvrh, z ktorého sa zistia informácie
     * @param doc   dokumentová štruktúra, kam sa má uložiť tabuľka s rozvrhom
     * @return prvý element tabuľky
     */
    public Element generujFOTabulkuRozvrh(Rozvrh.RozvrhMesiaca zapisovanyRozvrh,Document doc){
        if(doc == null || zapisovanyRozvrh == null)return null;
        Element elementTable = doc.createElement("fo:table");
        elementTable.setAttribute("font-family", "sans-serif");
        elementTable.setAttribute("font-size", FONT_SIZE+"pt");
        elementTable.setAttribute("font-weight", "bold");
        elementTable.setAttribute("table-layout", "fixed");
        
       
         //zaznamenanie poctu a siriek stlpcov
            int pocetDni = zapisovanyRozvrh.getInfo().getPocetDni();
            int sirkaBunky = (int)(297-4*CELL_WIDTH_HOD-CELL_WIDTH_MENO)/pocetDni;
            int pocetBuniek = pocetDni+5; 
            for(int i=1; i<=pocetBuniek;i++){
                Element elementTableInfoCol = doc.createElement("fo:table-column");
                elementTableInfoCol.setAttribute("column-number", ""+i);
                int sirkaStlpca = sirkaBunky;
                if(i == 1) sirkaStlpca = CELL_WIDTH_MENO;
                else if(i>=2 && i<=3) sirkaStlpca = CELL_WIDTH_HOD;
                else if(i>3+pocetDni) sirkaStlpca = CELL_WIDTH_HOD;
                
                elementTableInfoCol.setAttribute("column-width", ""+sirkaStlpca+"mm");
                elementTable.appendChild(elementTableInfoCol);
            }
            
        Element elementTableInfoBody = doc.createElement("fo:table-body");
        elementTableInfoBody.setAttribute("border-style", "solid");
        elementTableInfoBody.setAttribute("border-width", "0.1pt");
        
        for(Integer cislaRiadkov:zapisovanyRozvrh.getRiadkyZamestnancov().keySet()){
            Integer idZamestnanca = zapisovanyRozvrh.getRiadkyZamestnancov().get(cislaRiadkov);
            String priezviskoZamestnanca = zapisovanyRozvrh.getInfo().getMnozinaZamestnancov().get(idZamestnanca).getPriezviskoZamestnanca();
            String priezviskoDiakritika="";
            if(priezviskoZamestnanca.length()>7){
                priezviskoDiakritika = priezviskoZamestnanca.substring(0, 6);
                priezviskoDiakritika = this.generujStringSDiakritikou(priezviskoDiakritika);
            }else{
                priezviskoDiakritika = this.generujStringSDiakritikou(priezviskoZamestnanca);
            }
            
            
            Element elementTableRow = doc.createElement("fo:table-row");
            elementTableRow.setAttribute("line-height", ""+ROW_HEIGHT_ZAM+"mm");
            
            
            //bunka s identifikaciou
                Element idCell = doc.createElement("fo:table-cell");
                idCell.setAttribute("border-style", "solid");
                
                    Element block = doc.createElement("fo:block");
                    block.setAttribute("text-align", "center");
                    block.setTextContent(priezviskoDiakritika);
                    idCell.appendChild(block);
                
                elementTableRow.appendChild(idCell);
            
            // nadpocetne hodiny/ nedopocetne hodiny
                String minulostPlus = "";
                String minulostMinus = "";
                String sucasnostString ="";
                InformacnaStruktura.CasBezDatumu cas;
                int optMinuty = this.rozvrh.getInfo().getOptimalnyPocetMinut();
                Rozvrh.NastavenieZamestnancaMesiac nastavenie =zapisovanyRozvrh.getInfo().getNastavenieZamestnancov().get(idZamestnanca);
                if(nastavenie.getPrenostMinulehoObdobia().equals(new InformacnaStruktura.CasBezDatumu(0,0)) == false){
                    if(nastavenie.getTypSaldaMinulost()==InformacnaStruktura.TypSalda.NADPOCETNE){
                        minulostPlus = ""+nastavenie.getPrenostMinulehoObdobia();
                        cas = new InformacnaStruktura.CasBezDatumu(optMinuty-InformacnaStruktura.CasBezDatumu.celkovyPocetMinut(nastavenie.getPrenostMinulehoObdobia()));
                        sucasnostString = ""+cas.getHodiny()+" : "+cas.getMinuty();
                    }else{
                        cas = new InformacnaStruktura.CasBezDatumu(optMinuty+InformacnaStruktura.CasBezDatumu.celkovyPocetMinut(nastavenie.getPrenostMinulehoObdobia()));
                        sucasnostString = ""+cas.getHodiny()+" : "+cas.getMinuty();
                        minulostMinus = ""+nastavenie.getPrenostMinulehoObdobia();
                    }
                    
                }else{
                    cas = new InformacnaStruktura.CasBezDatumu(optMinuty);
                    sucasnostString = ""+cas.getHodiny()+" : "+cas.getMinuty();
                }
                Element nadpocetMinulostCell = doc.createElement("fo:table-cell");
                nadpocetMinulostCell.setAttribute("border-style", "solid");
                
                    Element blockPlusMinulost = doc.createElement("fo:block");
                    blockPlusMinulost.setAttribute("text-align", "center");
                    blockPlusMinulost.setTextContent(minulostPlus);
                    nadpocetMinulostCell.appendChild(blockPlusMinulost);
                
                elementTableRow.appendChild(nadpocetMinulostCell);
                
                Element nedopocetMinulostCell = doc.createElement("fo:table-cell");
                nedopocetMinulostCell.setAttribute("border-style", "solid");
                
                    Element blockMinusMinulost = doc.createElement("fo:block");
                    blockMinusMinulost.setAttribute("text-align", "center");
                    blockMinusMinulost.setTextContent(minulostMinus);
                    nedopocetMinulostCell.appendChild(blockMinusMinulost);
                
                elementTableRow.appendChild(nedopocetMinulostCell);
                
               
                
                //bunky dni mesiaca
                Zmeny[] zmeny = zapisovanyRozvrh.getRozvrh().get(idZamestnanca);
                String symbol="";
                for(int i = 1; i<= pocetDni;i++){
                    if(zmeny[i-1] == null) symbol="-";
                    else if(zmeny[i-1].getClass() == Rozvrh.ZmenaNastavitelnejDlzky.class){
                        symbol = ""+zmeny[i-1].getCasOd().getHodiny()+"/"+zmeny[i-1].getCasDo().getHodiny();
                    }else if(zmeny[i-1].getClass() == Rozvrh.StandardnaZmena.class)
                        switch(zmeny[i-1].getTypZmeny()){
                            case RANNA: {
                                symbol="R";
                                break;
                            }
                            case POOBEDNA:{
                                symbol = "P";
                                break;
                            }
                            case NOCNA:{
                                symbol = "N";
                                break;
                            }
                            case NOCNA_DLHA:{
                                symbol = "N";
                                break;
                            }
                            case KUCHYNA:{
                                symbol = "K";
                                break;
                            }
                            case DENNA:{
                                symbol="D";
                                break;
                            }
                            case DOVOLENKA:{
                                symbol = "DV";
                            }
                            
                        }
                    Element zmenaCell = doc.createElement("fo:table-cell");
                    zmenaCell.setAttribute("border-style", "solid");
                
                    Element blockZmena = doc.createElement("fo:block");
                    blockZmena.setAttribute("text-align", "center");
                    /*if(zmeny[i-1] != null && zmeny[i-1].getClass() == Rozvrh.ZmenaNastavitelnejDlzky.class){
                       blockZmena.setAttribute("text-size", "6pt");
                       blockZmena.setAttribute("text-family", "times");
                    }*/
                    blockZmena.setTextContent(symbol);
                   zmenaCell.appendChild(blockZmena);
                
                    elementTableRow.appendChild(zmenaCell);
                    
                    }//for
                
                    
                    
                    //nadpocetne nedopocetne hodiny buducnost
                    
                String buducnostPlus = "";
                String buducnostMinus = "";
                
                if(nastavenie.getPrenostBuducehoObdobia().getHodiny()!= 0 || nastavenie.getPrenostBuducehoObdobia().getMinuty() !=0){
                    if(nastavenie.getTypSaldaBuducnost()==InformacnaStruktura.TypSalda.NADPOCETNE){
                        buducnostPlus = ""+nastavenie.getPrenostBuducehoObdobia();
                    }else buducnostMinus = ""+nastavenie.getPrenostBuducehoObdobia();
                }
                Element buducnostPlusCell = doc.createElement("fo:table-cell");
                    buducnostPlusCell.setAttribute("border-style", "solid");
                
                    Element blockBuducnostPlus = doc.createElement("fo:block");
                    blockBuducnostPlus.setAttribute("text-align", "center");
                    blockBuducnostPlus.setTextContent(buducnostPlus);
                    buducnostPlusCell.appendChild(blockBuducnostPlus);
                
                elementTableRow.appendChild(buducnostPlusCell);
                
                Element buducnostMinusCell = doc.createElement("fo:table-cell");
                    buducnostMinusCell.setAttribute("border-style", "solid");
                
                    Element blockBuducnostMinus = doc.createElement("fo:block");
                    blockBuducnostMinus.setAttribute("text-align", "center");
                    blockBuducnostMinus.setTextContent(buducnostMinus);
                    buducnostMinusCell.appendChild(blockBuducnostMinus);
                
                elementTableRow.appendChild(buducnostMinusCell);
                
                
                
            elementTableInfoBody.appendChild(elementTableRow);
        }
        elementTable.appendChild(elementTableInfoBody);
        return elementTable;
        
    }
    /**
     * Metóda, ktorá pretransformuje textový reťazec, aby bol vhodný na zápis do PDF
     * @param string    textový reťazec, ktorý sa má pretransformovať
     * @return pretransformovaný textový reťazec
     */
    public String generujStringSDiakritikou(String string){
        String navrat = "";
        for(int i = 0 ; i< string.length(); i++){
            String kontrola = ""+string.charAt(i);
            switch(string.charAt(i)){
                case 'á':{
                    kontrola ="a";
                    break;
                }
                case 'Á':{
                    kontrola ="A";
                    break;
                }
                case 'ä':{
                    kontrola ="a";
                    break;
                }
                case 'Č':{
                    kontrola ="C";
                    break;
                }
                case 'č':{
                    kontrola ="c";
                    break;
                }
                case 'Ď':{
                    kontrola ="D";
                    break;
                }
                case 'ď':{
                    kontrola ="d";
                    break;
                }
                case 'É':{
                    kontrola ="E";
                    break;
                }
                case 'é':{
                    kontrola ="e";
                    break;
                }
                case 'Í':{
                    kontrola ="I";
                    break;
                }
                case 'í':{
                    kontrola ="i";
                    break;
                }
                case 'Ĺ':{
                    kontrola ="L";
                    break;
                }
                case 'ĺ':{
                    kontrola ="l";
                    break;
                }
                case 'Ľ':{
                    kontrola ="L";
                    break;
                }
                case 'ľ':{
                    kontrola ="l";
                    break;
                }
                case 'Ň':{
                    kontrola ="N";
                    break;
                }
                case 'ň':{
                    kontrola ="n";
                    break;
                }
                case 'Ó':{
                    kontrola ="O";
                    break;
                }
                case 'ó':{
                    kontrola ="o;";
                    break;
                }
                case 'ô':{
                    kontrola ="o";
                    break;
                }
                case 'Ŕ':{
                    kontrola ="R";
                    break;
                }
                case 'ŕ':{
                    kontrola ="r";
                    break;
                }
                case 'Ř':{
                    kontrola ="R";
                    break;
                }
                case 'ř':{
                    kontrola ="r";
                    break;
                }
                case 'Š':{
                    kontrola ="S";
                    break;
                }
                case 'š':{
                    kontrola ="s";
                    break;
                }
                case 'Ť':{
                    kontrola ="T";
                    break;
                }
                case 'ť':{
                    kontrola ="t";
                    break;
                }
                case 'Ú':{
                    kontrola ="U";
                    break;
                }
                case 'ú':{
                    kontrola ="u";
                    break;
                }
                case 'Ý':{
                    kontrola ="Y";
                    break;
                }
                case 'ý':{
                    kontrola ="y";
                    break;
                }
                case 'Ž':{
                    kontrola ="Z";
                    break;
                }
                case 'ž':{
                    kontrola ="z";
                    break;
                }
               
                      
            }
            navrat += kontrola;
        }
        
        return navrat;
    }
    /**
     * Metóda, ktorá vygeneruje dokument formátu XSL-FO obashujúci rozvrh, vhodný na transformáciu do PDF
     * @return true, pokiaľ sa dakument vygeneroval, false inak
     */
    public boolean generujFO(){
         try {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                InputStream in = this.getClass().getClassLoader().getResourceAsStream("fopConfig/zakladnaSablona.xml");
                
                //File xmlZaklad = new File("./src/fopConfig/zakladnaSablona.xml");
                
                Document doc = docBuilder.parse(in);
                doc.getDocumentElement().normalize();
		
                 NodeList nList = doc.getElementsByTagName("fo:table");
                 if(nList == null || nList.getLength() !=2){
                     System.out.println("Doslo k chybe s nacitanim zakladnej sablony.");
                     return false;
                 }
                 Element foTabulkaInfo = this.generujFOTabulkuInfo(this.rozvrh, doc);
                 Element foTabulkaZmeny = this.generujFOTabulkuRozvrh(this.rozvrh, doc);
                 
                 Node nodeTabulkaInfo = (Node) foTabulkaInfo;
                 Node nodeTabulkaZmeny = (Node) foTabulkaZmeny;
                 
                 Node parentNode = nList.item(0).getParentNode();
                 parentNode.insertBefore(nodeTabulkaInfo, nList.item(1));
                 nList = doc.getElementsByTagName("fo:table");
                 parentNode.insertBefore(nodeTabulkaZmeny, nList.item(2));
                
                
                
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		this.source = new DOMSource(doc);
                
                File resultFile =  new File(".\\"+this.rozvrh.getInfo().getMasiac()+".fo");
                resultFile.createNewFile();
		StreamResult result = new StreamResult(resultFile.toURI().getPath());
               // System.out.prinln("Pre");
                transformer.transform(source, result);
         } catch (ParserConfigurationException pce) {
		pce.printStackTrace();
	  } catch (TransformerException tfe) {
		tfe.printStackTrace();
	  }catch (Exception e){
              e.printStackTrace();
          }
        
        return false;
    }
    /**
     * Metóda, ktorá generuje PDF z formátu XSL-FO
     * @throws TransformerConfigurationException
     * @throws FileNotFoundException
     * @throws FOPException
     * @throws TransformerException
     * @throws ConfigurationException
     * @throws IOException
     * @throws SAXException 
     */
    public void generujPDF() throws TransformerConfigurationException,FileNotFoundException, FOPException, TransformerException, ConfigurationException, IOException, SAXException{
        if(this.source == null)return;
        FopFactory fopFactory = FopFactory.newInstance();
        OutputStream out = null; 
        try {
            //konfiguracia fop
            DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
            //InputStream in = this.getClass().getClassLoader().getResourceAsStream("fopConfig/configFonts.xml");
                
                //File xmlZaklad = new File("./src/fopConfig/zakladnaSablona.xml");
            //File config = new File(in);
            //Document doc = docBuilder.parse(in);
           // Configuration cfg = cfgBuilder.buildFromFile(new File("./src/fopConfig/configFonts.xml"));
            //fopFactory.setUserConfig(cfg);
            String nazovDokumentu = this.rozvrh.getInfo().getRok()+""+this.rozvrh.getInfo().getMasiac();
            File pdfFile;
            pdfFile = new File(nazovDokumentu+".pdf");
            
            int i = 0;
            while(pdfFile.createNewFile() == false){
                i++;
                pdfFile = new File(nazovDokumentu+"("+i+").pdf");
            }
            //if(pdfFile.canWrite() == false){
            //     JOptionPane.showMessageDialog(null,"Súbor, do ktorého chcete zapisovať je otvorený iným programom. Nemožno zapisovať.");
             //    return;
            //}
            
            //pdfFile2.createNewFile();
            FileOutputStream fosPdfFile = new FileOutputStream(pdfFile);
            out = new BufferedOutputStream(fosPdfFile);
             //Fop fop = fopFactory.
             FOUserAgent uAgent = new FOUserAgent(fopFactory);
             //uAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
             Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, uAgent, out);
             

        
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer

        Source src = new StreamSource(new File(".\\"+this.rozvrh.getInfo().getMasiac()+".fo"));

       
        Result res = new SAXResult(fop.getDefaultHandler());

       
        transformer.transform(src, res);
   
        FormattingResults foResults = fop.getResults();
        java.util.List pageSequences = foResults.getPageSequences();
        for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
            PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
	System.out.println("PageSequence "
 	+ (String.valueOf(pageSequenceResults.getID()).length() > 0
	? pageSequenceResults.getID() : "<no id>")
	+ " generated " + pageSequenceResults.getPageCount() + " pages.");
	}
	System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
        

        }catch( FileNotFoundException ex){
            JOptionPane.showMessageDialog(null,"Súbor, do ktorého chcete zapisovať je otvorený iným programom. Nemožno zapisovať.");
        } 
        finally {
        System.out.println(out);
         if(out != null)out.close();
}
        }
    
    
}